package com.treasure.infrastructure.domain.impl;

import com.treasure.domain.domain.TextVariablesCombinedDomainService;
import com.treasure.domain.domain.entity.PromptTemplateContext;
import com.treasure.domain.domain.entity.VerifyMessageContext;
import com.treasure.domain.domain.entity.document.CreateAccountNotify;
import com.treasure.domain.velocity.TemplateEngine;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;

/**
 * @author sunhongmin
 * @date 2023/11/5 12:31
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class TextVariablesCombinedDomainServiceImpl implements TextVariablesCombinedDomainService {
    private final TemplateEngine templateEngine;


    @Override
    public String promptTemplate(PromptTemplateContext promptTemplateContext) throws IOException {
        return templateEngine.writeDocument("BaseAnswerPrompt.vm", new HashMap<String, Object>() {{
            put("segments", promptTemplateContext.getSegmentDocList());
            put("question", promptTemplateContext.getQuestion());
        }});
    }

    @Override
    public String verifyMessageTemplateWithSms(VerifyMessageContext verifyMessageContext) throws IOException {
        return templateEngine.writeDocument("VerifyMsgSMSTemplate.vm", new HashMap<String, Object>() {{
            put("code", verifyMessageContext.getCode());
            put("time", verifyMessageContext.getValidityPeriod());
        }});
    }

    @Override
    public String verifyMessageTemplateWithEmail(VerifyMessageContext verifyMessageContext) throws IOException {
        return templateEngine.writeDocument("VerifyMsgEmailTemplate.vm", new HashMap<String, Object>() {{
            put("code", verifyMessageContext.getCode());
            put("time", verifyMessageContext.getValidityPeriod());
        }});
    }

    @Override
    public String createAccountTemplate(CreateAccountNotify createAccountNotify) throws IOException, IllegalAccessException {
        return templateEngine.writeDocument("CreateAccountNotifyTemplate.vm", createAccountNotify);
    }
}
